<?php

namespace Api\Controller;

use Think\Controller;


class IndexController extends PublicController
{
    //查找分类
   public function car(){
        $list = M('menu')->select();
        echo json_encode($list);
   }
   //分类下id
   public function carlist(){
        $id = I('request.id');  
        if ($id == 1) {
            $where['c.tj'] = array('eq',1);
        }else{
            $where['c.carid'] = array('eq',$id);
        }
        $where['c.status'] = array('eq',0);
        $list = M('ceping')
                ->alias('c')
                ->field('c.*,u.nickname')
                ->join('left join xier_user as u on u.id=c.uid')
                ->where($where)
                ->select(); 
        echo json_encode($list);
   }
   //点赞
   public function dz(){
        $id = I('request.id');
        $uid = I('request.uid');
        $cpwhere['id'] = array('eq',$id);
        $data = M('ceping')->where($cpwhere)->find();
        if ($data) {
            $dz['dz'] = $data['dz']+1;
            $save = M('ceping')->where($cpwhere)->save($dz);
            $map['uid'] = $uid;
            $map['cid'] = $id;
            $add = M('dz')->add($map);
            if ($add) {
                echo json_encode('1');
            }else{
                echo json_encode('0');
            }
        }else{
            echo json_encode('0');
        }
   }
    //转发
   public function zf(){
        $id = I('request.id');
        $uid = I('request.uid');
        $cpwhere['id'] = array('eq',$id);
        $data = M('ceping')->where($cpwhere)->find();
        if ($data) {
            $dz['zf'] = $data['zf']+1;
            $save = M('ceping')->where($cpwhere)->save($dz);
            $map['uid'] = $uid;
            $map['cid'] = $id;
            $add = M('zf')->add($map);
            if ($add) {
                echo json_encode('1');
            }else{
                echo json_encode('0');
            }
        }else{
            echo json_encode('0');
        }
   }
    //收藏
   public function sc(){
        $id = I('request.id');
        $uid = I('request.uid');
        $cpwhere['id'] = array('eq',$id);
        $data = M('ceping')->where($cpwhere)->find();
        if ($data) {
            $dz['sc'] = $data['sc']+1;
            $save = M('ceping')->where($cpwhere)->save($dz);
            $map['uid'] = $uid;
            $map['cid'] = $id;
            $add = M('sc')->add($map);
            if ($add) {
                echo json_encode('1');
            }else{
                echo json_encode('0');
            }
        }else{
            echo json_encode('0');
        }
   }

   //新建测评 
   public function cpadd(){
    $uid = I('request.uid');
    $title = I('request.title');
    $cetype = I('request.cetype');
    $con = I('request.con');
    $zjname = I('request.zjname');
    $hd = I('request.hd');
    $status = I('request.status');
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     113145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg',"RM","RMVB","AVI","WMV","MPG ","MPEG","FLV","3GP","MP4","SWF","ASF","DIVX","XVID","3GP2","FLV1","MPEG1","MPEG2","MPEG3","MPEG4","H264",'rm','rmvb','avi','wmv','mpg','mpeg','flv','3gp','mp4','swf','asf','divx','xvid','3gp2','flv1','mpeg1','mpeg2','mpeg3','mpeg4','h264');// 设置附件上传类型
    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
    $upload->savePath  =     'mov/'; // 设置附件上传（子）目录
    // 上传文件 
    $info   =   $upload->upload();
    if(!$info) {// 上传错误提示错误信息
        echo json_encode('0');die;
    }else{// 上传成功
        $data['uid'] = $uid;
        $data['titlt'] = $title;
        $data['cetype'] = $cetype;
        $data['con'] = $con;
        $data['zjname'] = $zjname;
        if ($status) {
            $data['status'] = $status;
        }
        $data['date'] = date('Y-m-d H:i:s',time());
        if ($hd) {
           $data['hd'] = $hd;
        }
        $add = M('ceping')->add($data);
        $savei['cid'] = $add;
        $i =0;
        foreach ($info as $k => $v) {
            $savei['url'] = '';
            if ($i==0) {
                $cpwhere['id'] = $add;
                $map['img'] = 'Uploads/'.$v['savepath'].$v['savename'];
                $save = M('ceping')->where($cpwhere)->save($map);
            }
            $savei['url'] = 'Uploads/'.$v['savepath'].$v['savename'];
            $addsave = M('img')->add($savei);
            $i++;
        }
        echo json_encode('1');die;
    }
   }

   //新增评论
   public function pinglunadd(){
    $uid = I('request.uid');
    $con = I('request.con');
    $cid = I('request.cid');
    $map['uid'] = $uid;
    $map['con'] = $con;
    $map['cid'] = $cid;
    $data = M('pinglun')->add($map);
    if ($data) {
        echo json_encode('1');
    }else{
        echo json_encode('0');
    }
   }

   //每日签到
   public function qiandao(){
    $uid = I('request.uid');
    $where['id'] = array('eq',$uid);
    $data = M('user')->where($where)->find();
    $save['jb'] = $data['jb']+10;
    $jb = M('user')->where($where)->save($save);
    if ($jb) {
        echo json_encode('1');
    }else{
        echo json_encode('0');
    }
   }
   //
}